home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8340 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  84 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Need HELP by a C hacker....
  5. Date: 01 Mar 1996 05:42:23 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb29224223@qcd.lanl.gov>
  8. References: <4h5mhv$mh9@newsstand.cit.cornell.edu>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: NOBODY's message of 1 Mar 1996 02:14:23 GMT
  13.  
  14. In article <4h5mhv$mh9@newsstand.cit.cornell.edu>
  15. NOBODY <rezab@nova.npac.syr.edu> writes:
  16.  
  17. N: I want to declare a function
  18. <snip>
  19. N: _____ foo(void){
  20. N:        return foo;
  21. N:   }
  22. N: 
  23. N: Ususally _____ is the type of the thing that I am returning.  
  24. N: In this case it's a pointer to a function that returns a pointer
  25. N: to a function.  Does anyone have any ideas?  
  26.  
  27. This is a flaw in the type system of C. It does not have the nice
  28. yin-yang rules, and so, it is not possible to do what you want to do.
  29.  
  30. There are basically two approaches: return a struct:
  31.  
  32. struct function { struct function (*fun)(void); }
  33.        foo(void) {
  34.          static struct function retval;
  35.          retval.fun = foo;
  36.          return retval;
  37.   }
  38.  
  39. And then use foo().fun in the same way that you would normally have
  40. used foo() in your `version' of the code.
  41.  
  42. Else, you can use casts: as any pointer to a function can be cast to
  43. any other (and cast back before calling), you can write
  44.  
  45. int (*foo(void))(void) {
  46.   return (int(*)(void))foo;
  47. }
  48.  
  49. and use ((int(*(*)(void))(void))foo()) instead of foo(). On second
  50. thoughts, use typedefs to achieve the same purpose :-)
  51.  
  52. I prefer the first solution even when the second is made even slightly
  53. legible even to experienced programmers.
  54.  
  55. N: I have gone nuts trying to figure this one out.  
  56. N: BTW, I am using the gcc compiler (My microsoft compiler does not allow
  57. N: anything like this to be declared).  
  58.  
  59. As I said, C typesystem is flawed in this regard. I would really have
  60. liked an incomplete function type:
  61.  
  62. () functype;
  63. typedef functype *functype(void);
  64. where the first line is for ease of parsing, and declares functype to
  65. be a `function type' to be completed later; and the second is the
  66. completion. `typedef functype' would have been nice except for
  67. historical assumption of `int' as the default type; only the second is
  68. unacceptable because if functype exists as a typedef in an enclosing
  69. scope, we need ambiguity resolution rules which really make life
  70. harder for everyone. A `function' keyword would solve the problem
  71. neatly, but new keywords are never backward compatible.
  72.  
  73. This, however, is not C.
  74.  
  75. Cheers
  76. Tanmoy
  77. --
  78. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  79. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  80. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  81. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  82. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  83. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  84.